package com.qingcheng.task;

import com.qingcheng.dao.SeckillGoodsMapper;
import com.qingcheng.pojo.seckill.SeckillGoods;
import com.qingcheng.pojo.seckill.SeckillOrder;
import com.qingcheng.pojo.seckill.SeckillStatus;
import com.qingcheng.util.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class MultiThreadingCreateOrder {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private SeckillGoodsMapper seckillGoodsMapper;
    @Autowired
    private IdWorker idWorker;
    /***
     * 多线程下单操作
     */
    @Async//异步多线程
    public void createOrder(){
        try {
            Thread.sleep(2000);

            //获取排队信息
            SeckillStatus seckillStatus =(SeckillStatus) redisTemplate.boundListOps("SeckillOrderQueue").rightPop();

            String username=seckillStatus.getUsername();
            Long id = seckillStatus.getGoodsId();
            String time = seckillStatus.getTime();

            //从redis中获取商品
            Object ids = redisTemplate.boundListOps("SeckillGoodsCountList_"+id).rightPop();
            if (ids==null){
                //卖完了
                //清理排队信息

            }
            //获取商品信息
            SeckillGoods goods = (SeckillGoods) redisTemplate.boundHashOps("SeckillGoods_"+time).get(id);
            //如果没有库存，则直接抛出异常
            if (goods == null || goods.getStockCount() <= 0) {
                throw new RuntimeException("已售罄!");
            }
            //如果有库存，则创建秒杀商品订单
            SeckillOrder seckillOrder = new SeckillOrder();
            seckillOrder.setId(idWorker.nextId());
            seckillOrder.setSeckillId(id);
            seckillOrder.setMoney(goods.getCostPrice());
            seckillOrder.setUserId(username);
            seckillOrder.setSellerId(goods.getSellerId());
            seckillOrder.setCreateTime(new Date());
            seckillOrder.setStatus("0");
            //将秒杀订单存入到Redis中
            redisTemplate.boundHashOps("SeckillOrder").put(username, seckillOrder);
            //库存减少
            goods.setStockCount(goods.getStockCount() - 1);
            //判断当前商品是否还有库存
            if (goods.getStockCount() <= 0) {
                //并且将商品数据同步到MySQL中
                seckillGoodsMapper.updateByPrimaryKeySelective(goods);
                //如果没有库存,则清空Redis缓存中该商品
                redisTemplate.boundHashOps("SeckillGoods_" + time).delete(id);
            } else {
                //下单成功
                //如果有库存，则直数据重置到Reids中
                redisTemplate.boundHashOps("SeckillGoods_" + time).put(id, goods);
                //修改用户排队下单信息，修改数据主要修改用户下单状态，已下单待支付
                seckillStatus.setMoney(seckillOrder.getMoney().floatValue());
                seckillStatus.setOrderId(seckillOrder.getId());
                seckillStatus.setStatus(2);
                redisTemplate.boundHashOps("UserQueueStatus").put(username,seckillStatus);

            }

        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }


}
